﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace FranceTriple.Dal
{
    public class ForumCategoryView
    {
        private static XElement xelem;
        private static List<Int32> Ids;

        public static String GetXmlEntities(String AnonymousID, String requesterTimeZone)
        {
            xelem = null;
            Ids = UserAccessDal.ForumIdAllowed(AnonymousID);

            try
            {
                using (FranceTriple.Dal.Model1Container ctx = new Model1Container())
                {
                    var query = (from f1 in
                                     (from f in ctx.ForumSet.Include("ForumCategory")
                                      where Ids.Contains(f.Id)
                                      orderby f.OrderForum
                                      select f).ToList()
                                 select new XElement("Forum",
                                     new XAttribute("Id", f1.Id),
                                     new XAttribute("Label", f1.LabelForum),
                                     new XAttribute("Order", f1.OrderForum),
                                     from c in f1.ForumCategory
                                     orderby c.OrderForumCategory
                                     select new XElement("ForumCategory",
                                         new XAttribute("Id", c.Id),
                                         new XAttribute("Label", c.LabelForumCategory),
                                         new XAttribute("Order", c.OrderForumCategory),
                                         new XAttribute("Description", c.DescriptionForumCategory ?? String.Empty),
                                         new XAttribute("NumberOfTopic", c.NumberOfTopic),
                                         new XAttribute("NumberOfPost", c.NumberOfMessage),
                                         new XAttribute("LastPostId", c.LastPostId ?? 0),
                                         new XAttribute("LastPostCreationDate", (c.LastPostCreationDate ?? new DateTime()).ToUserTimeZone(requesterTimeZone).ToString()),
                                         new XAttribute("LastPostAuthor", c.LastPostAuthor ?? ""),
                                         new XAttribute("LastPostTopicId", c.LastPostTopicId ?? 0),
                                         new XAttribute("Moderators",UserAccessDal.GetModerators(c.Id))
                                         )));

                    xelem = new XElement("o", query);
                }
            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new DalException(String.Format("RetrieveError".ToLocalizedString(), "GetXmlEntities", ex.Message), ex) { EntityType = typeof(Forum[]) };
            }
            return xelem.ToString();
        }
    }
}

